Grokking Dynamic Programming Patterns for Coding Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
What is Dynamic Programming?
Pattern 1: 0/1 Knapsack
Introduction
0/1 Knapsack
Solution: 0/1 Knapsack
Equal Subset Sum Partition
Solution: Equal Subset Sum Partition
Subset Sum
Solution: Subset Sum
Minimum Subset Sum Difference
Solution: Minimum Subset Sum Difference
Count of Subset Sum
Solution: Count of Subset Sum
Target Sum
Solution: Target Sum
Pattern 2: Unbounded Knapsack
Unbounded Knapsack
Solution: Unbounded Knapsack
Rod Cutting
Solution: Rod Cutting
Coin Change
Solution: Coin Change
Minimum Coin Change
Solution: Minimum Coin Change
Maximum Ribbon Cut
Solution: Maximum Ribbon Cut
Pattern 3: Fibonacci Numbers
Fibonacci numbers
Solution: Fibonacci numbers
Staircase
Solution: Staircase
Number factors
Solution: Number factors
Minimum jumps to reach the end
Solution: Minimum jumps to reach the end
Minimum jumps with fee
Solution: Minimum jumps with fee
House thief
Solution: House thief
Pattern 4: Palindromic Subsequence
Longest Palindromic Subsequence
Solution: Longest Palindromic Subsequence
Longest Palindromic Substring
Solution: Longest Palindromic Substring
Count of Palindromic Substrings
Solution: Count of Palindromic Substrings
Minimum Deletions in a String to make it a Palindrome
Solution: Minimum Deletions in a String to make it a Palindrome
Palindromic Partitioning
Solution: Palindromic Partitioning
Pattern 5: Longest Common Substring
Longest Common Substring
Solution: Longest Common Substring
Longest Common Subsequence
Solution: Longest Common Subsequence
Minimum Deletions & Insertions to Transform a String into another
Solution: Minimum Deletions & Insertions to Transform a String into another
Longest Increasing Subsequence
Solution: Longest Increasing Subsequence
Maximum Sum Increasing Subsequence
Solution: Maximum Sum Increasing Subsequence
Shortest Common Super-sequence
Solution: Shortest Common Super-sequence
Minimum Deletions to Make a Sequence Sorted
Solution: Minimum Deletions to Make a Sequence Sorted
Longest Repeating Subsequence
Solution: Longest Repeating Subsequence
Subsequence Pattern Matching
Solution: Subsequence Pattern Matching
Longest Bitonic Subsequence
Solution: Longest Bitonic Subsequence
Longest Alternating Subsequence
Solution: Longest Alternating Subsequence
Edit Distance
Solution: Edit Distance
Strings Interleaving
Solution: Strings Interleaving
Contact Us
Solution: Subsequence Pattern Matching

Problem Statement

Given a string and a pattern, write a method to count the number of times the pattern appears in the string as a subsequence.

Example 1: Input: string: "baxmx", pattern: "ax"
Output: 2
Explanation: {baxmx, baxmx}.

Example 2:

Input: string: "tomorrow", pattern: "tor"
Output: 4
Explanation: Following are the four occurences: {tomorrow, tomorrow, tomorrow, tomorrow}.

Basic Solution

This problem follows the "Longest Common Subsequence" (LCS) pattern and is quite similar to the "Longest Repeating Subsequence"; the difference is that we need to count the total occurrences of the subsequence.

A basic brute-force solution could be to try all the subsequences of the given string to count all that match the given pattern. We can match the pattern with the given string one character at a time, so we can do two things at any step:

  1. If the pattern has a matching character with the string, we can recursively match for the remaining lengths of the pattern and the string.
  2. At every step, we can always skip a character from the string to try to match the remaining string with the pattern. So we can start a recursive call by skipping one character from the string.

Our total count will be the sum of the counts returned by the above two options.

Here is the code:

Java
Saved to Cloud

. . . .

The time complexity of the above algorithm is exponential , where ‘m’ is the length of the string, as our recursion stack will not be deeper than m. The space complexity is which is used to store the recursion stack.

Top-down Dynamic Programming with Memoization

We can use an array to store the already solved subproblems.

The two changing values to our recursive function are the two indexes strIndex and patIndex. Therefore, we can store the results of all the subproblems in a two-dimensional array. (Another alternative could be to use a hash-table whose key would be a string (strIndex + "|" + patIndex)).

Here is the code:

Java

. . . .

Bottom-up Dynamic Programming

Since we want to match all the subsequences of the given string, we can use a two-dimensional array to store our results. As mentioned above, we will be tracking separate indexes for the string and the pattern, so we will be doing two things for every value of strIndex and patIndex:

  1. If the character at the strIndex (in the string) matches the character at patIndex (in the pattern), the count of the SPM would be equal to the count of SPM up to strIndex-1 and patIndex-1.
  2. At every step, we can always skip a character from the string to try matching the remaining string with the pattern; therefore, we can add the SPM count from the indexes strIndex-1 and patIndex.

So our recursive formula would be:

if str[strIndex] == pat[patIndex] {
  dp[strIndex][patIndex] = dp[strIndex-1][patIndex-1]
}
dp[strIndex][patIndex] += dp[strIndex-1][patIndex]

Code

Here is the code for our bottom-up dynamic programming approach:

Java

. . . .

The time and space complexity of the above algorithm is , where ‘m’ and ‘n’ are the lengths of the string and the pattern respectively.

Subsequence Pattern Matching
Longest Bitonic Subsequence
Mark as Completed

On this page

Problem Statement

Basic Solution

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Code